<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" style="width: 100px" size="small" @change="dateChange">
        <el-option v-for="year in yearList" :key="year" :value="year" :label="year+'年'" />
      </el-select>
      <el-select v-model="currentMonth" style="width: 88px" size="small" @change="dateChange">
        <el-option v-for="month in 12" :key="month" :value="month" :label="month+'月'" />
      </el-select>
    </el-row>

    <el-calendar
      v-model="currentDate"
    >
      <template #dateCell="obj">
        <div class="date-content">
          <span class="text"> {{ obj.data.day | getDay }}</span>
          <span v-if="isWeek(obj.date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(oldDay) {
      // 2002-03-26
      return oldDay.split('-')[2]
    }
  },
  data() {
    const currentDate = new Date()
    const currentYear = currentDate.getFullYear()
    const currentMonth = currentDate.getMonth() + 1
    const yearList = []

    for (let index = 0; index < 11; index++) {
      // 以五年前年份开始, 每次加一生成11年
      // 其实就是以当前年份为中心, 向前推5年 向后推5年
      yearList.push(currentYear - 5 + index)
    }

    return {
      currentDate,
      currentYear,
      currentMonth,
      yearList
    }
  },
  watch: {
    currentDate() {
      this.currentYear = this.currentDate.getFullYear()
      this.currentMonth = this.currentDate.getMonth() + 1
    }
  },
  methods: {
    dateChange() {
      // 每当年份和月份收到改变, 就应该重新计算日历的当前日期
      // 生成日期的办法如下 new Date('2023-12')
      this.currentDate = new Date(this.currentYear + '-' + this.currentMonth)
    },
    isWeek(date) {
      // 日起对象是星期几自带 api data.getDay
      // 星期一到六结果分别是 1-6, 星期天是 0
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style scoped>
 /deep/ .el-calendar-day {
  height:  auto;
 }
 /deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 /deep/ .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 /deep/ .el-calendar__header {
   display: none
 }
</style>
